package com.loie.xcx.web;

import com.loie.xcx.comm.SeqEnum;
import lombok.AllArgsConstructor;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.util.Objects;

/**
 * @author zhl
 */
@Component
@AllArgsConstructor
public class ApplicationInit implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) throws IOException {
        System.out.println("*******************初始化执行开始*******************");
        initSeq();
        System.out.println("*******************初始化执行结束*******************");
    }


    private Environment environment;

    private void initSeq() throws IOException {
        String path = environment.getProperty("seq.path");
        File file = new File(Objects.requireNonNull(path));
        if (file.exists() && file.isDirectory()) {
            for (SeqEnum s : SeqEnum.values()) {
                file = new File(path + "/" + s + ".txt");
                if (file.createNewFile()) {
                    System.out.println("SEQ==>" + path + "/" + s + ".txt" + "文件创建成功");
                }
            }
        } else {
            System.out.println("ERROR：SEQ目录不存在==>" + path);
            throw new IOException("ERROR：SEQ目录不存在==>" + path);
        }
    }
}
